
var TaskStatus = {
    GOOD : {
        code : 1,
        message :  "<br>测试正常"
    },
    RUNNING :{
        code : 2,
        message : "<br>正在测试中"
    },
    WAITING : {
        code : 3,
        message : "<br>等待测试"
    },
    FAIL : {
        code : 4,
        message : "测试失败 <br>"
    }
}
var mainLogFlashInterval;
var Ui = {
    updateMainLog : function(isError,log)   {

        var mainLog = $("#main-log");
        var nav = mainLog.parent();
        nav.addClass('green');
        nav.removeClass('red');
        if(!log){
            log = isError;
        }
        else {
            if(isError){
                nav.removeClass('green');
                nav.addClass('red');
            }
        }
        mainLog.children().html(log);
    },
    appendLog : function(isError,log){
        if(!log){
            log = isError;
        }
        var logContent = $(".log-content");
        logContent.prepend("<p>" + log + "</p>");
    },
    updateTaskStatus : function(taskId,status ,reason){
        var task = $("#task-" + taskId);
        var circle = task.children('.circle');
        var info = task.children(".info");
        var taskProcess = task.children(".task-process");
        switch (status.code){
            case TaskStatus.GOOD.code:
                circle.removeClass("red");
                circle.removeClass("cyan");
                circle.addClass("green");
                circle.html("done");
                info.html(TaskStatus.GOOD.message);
                taskProcess.addClass('hide');
                break;
            case TaskStatus.RUNNING.code:
                circle.removeClass("red");
                circle.removeClass("green");
                circle.addClass("cyan");
                circle.html("loop");
                info.html(TaskStatus.RUNNING.message);
                taskProcess.removeClass('hide');
                break;
            case TaskStatus.FAIL.code:
                circle.removeClass("green");
                circle.removeClass("cyan");
                circle.addClass("red");
                circle.html("warning");
                if(reason){
                    reason = TaskStatus.FAIL.message + reason;
                }
                else {
                    reason = "<br>" +  TaskStatus.FAIL.message ;
                }
                info.html(reason);
                //taskProcess.addClass('hide');
                break;
            case TaskStatus.WAITING.code:
                circle.removeClass("red");
                circle.removeClass("green");
                circle.removeClass("cyan");
                info.html(TaskStatus.WAITING.message);
                taskProcess.addClass('hide');
                break;
            default :
                return;
        }
    },
    initSerial : function(arr ){
        var select = $("#serial-select");
        $.each(arr,function(i,v){
            select.append(' <option value="' + v+ '">' + v + '</option>');
        });
        select.material_select();
    },
    initLog : function(){

        $(".log-content").html("")
        $("#clean-log").click(function(e){
            e.preventDefault();
            var isClean = confirm("确定要清除当前的日志吗？");
            if(isClean){
                $(".log-content").html("")
            }
        });
    },
    initTask : function(list){
        var collection = $("#task-collection");
        collection.html("");
        var baseTask = $("#base-task");
        for(var i = 0; i < list.length;i++){

            if(list[i].enable){
                var clone = baseTask.clone();
                clone.attr("id" ,"task-" + list[i].id);
                clone.find(".title").html(list[i].name);
                clone.removeClass("hide");
                collection.append( clone);

            }
        }

    },
    flashMainLog : function(start){
        var mainLog = $("#main-log");
        var son = $("#main-log").children();
        if(start){
            var a = 0;
            mainLogFlashInterval = setInterval(function(){

                var color ;
                if(mainLog.parent().hasClass('green')){
                    color = 'green-text';
                }
                else if(mainLog.parent().hasClass('red')){
                    color = 'red-text';
                }
                a++
                if( a % 2 ){
                    son.removeClass('red-text');
                    son.removeClass('green-text');
                }
                else {
                    son.addClass(color);
                }

            },700) ;
        }
        else {
            if(mainLogFlashInterval){
                clearInterval(mainLogFlashInterval);
                son.removeClass('red-text');
                son.removeClass('green-text');
            }

        }
    },
    updatePlugStatus : function(status){
        var plug = $("#plug-status");
        if(status){
           plug.html('settings_input_hdmi');
        }
        else {
            plug.html('settings_input_composite');
        }
    },
    updateTaskRate: function(taskId , percent){
        var task = $("#task-" + taskId);
        var circle = task.children('.circle');
        var info = task.children(".info");
        var taskProcess = task.children(".task-process");
        taskProcess = taskProcess.children("div");
        taskProcess.removeClass("indeterminate");
        taskProcess.addClass("determinate");
        taskProcess.attr("style","width: " + percent + "%");


    }




};
